<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 14:46
 */

class Involved_StorageModel extends BaseModel
{
    protected $table = 'storage';

    /*********************  App接口  *****************/

    /**
     * 仓储列表
     * @param $type
     * @return mixed
     * @author alice
     * @date 2016-1-23
     */
    public function GetStorageList($type)
    {
        return DB::table($this->table)
                ->select('storage.storage_id','storage.storage_name','storage.show_pic1','storage.storage_address','storage.rank','storage.region_id',
                    'region.region_name')
                ->leftJoin('region', 'region.region_id', '=', 'storage.region_id')
                ->where('storage.country_type',$type)
                ->get();
    }

    /**
     * 仓储地详情
     * @param $storage_id
     * @return array
     * @throws Exception
     * @author alice
     * @date 2016-1-23
     */
    public function GetStorageDetail($storage_id)
    {
        $storage =  DB::table($this->table)
            ->select('storage_id','storage_name','quote','storage_address',
                'show_pic1','show_pic2','show_pic3','introduce','video_id','other_fee')
            ->where(['storage_id'=>$storage_id])
            ->first();

        if(empty($storage))
            throw new Exception('仓储基地不存在 ', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);

        $storage->introduce = strip_tags($storage->introduce);

        $data = [
            'storage_id'                => $storage->storage_id,
            'storage_name'              => $storage->storage_name,
            'quote'                     => $storage->quote,
            'storage_address'           => $storage->storage_address,
            'introduce'                 => strip_tags($storage->introduce),
            'video_id'                  => $storage->video_id,
            'img'               =>[
                $storage->show_pic1,
                $storage->show_pic2,
                $storage->show_pic3,
            ],
            'video_url'         => HostEnum::HOST_NAME.'/Api/video_play?type_id='.HostEnum::VIDEO_TYPE_STORAGE.'&video_id='.$storage->video_id
        ];
        return $data;

    }

    /**
     * 获取仓储地下拉列表
     * @return mixed
     * @author alice
     * @date 2016-2-24
     */
    public function GetProductStorageList()
    {
        $storages = DB::table($this->table)
            ->select('storage_id','storage_name')
            ->get();

        $info = [];
        foreach ($storages as $storage) {
            $info[] = [
                'id'    =>  $storage->storage_id,
                'name'  =>  $storage->storage_name,
            ];
        }

        return $info;
    }






    /*********************  PC端  *****************/

    /**
     * 仓储地列表
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowStorage()
    {
        return  DB::table($this->table)->paginate(8);
    }

    /**
     * 仓储地详情
     * @param $id
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowStorageInfo($id)
    {
        return  DB::table($this->table)->where('storage_id',$id)->first();
    }



}